package com.xiongkw;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

public class TT {
    public static void main(String[] args) {

        ArrayList<TimeRange> bRangeList = Lists.newArrayList(
                new TT().new TimeRange(0, 3),
                new TT().new TimeRange(6, 8),
                new TT().new TimeRange(8, 11)
        );

        ArrayList<TimeRange> oRangeList = Lists.newArrayList(
                new TT().new TimeRange(1, 5),
                new TT().new TimeRange(7, 10),
                new TT().new TimeRange(11, 14)
        );

        long start = System.currentTimeMillis();
        cruNew(bRangeList,oRangeList);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }


    public static void cruNew(ArrayList<TimeRange> bRangeList, ArrayList<TimeRange> oRangeList ){
        List<TimeRange> missedTimeRanges = new ArrayList<>();
        for(TimeRange b : bRangeList){
            TimeRange calRange = new TT().new TimeRange(b.start,b.end);
            boolean finded = false;
            List<TimeRange> matchedRanges = oRangeList.stream().filter(o -> o.end >= calRange.start && o.start <= calRange.end).collect(Collectors.toList());
            Collections.sort(matchedRanges);
            if(CollectionUtils.isNotEmpty(matchedRanges)){
                finded = true;
                for(TimeRange o : matchedRanges){
                    if(o.start <= calRange.start && o.end >= calRange.end){
                        calRange.start = o.end;
                        break;
                    }

                    if(o.start <= calRange.start && o.end < calRange.end){//缺了后一段
                        calRange.start = o.end;
                        continue;
                    }

                    if(o.start > calRange.start && o.end >= calRange.end){//缺了前一段
                        missedTimeRanges.add(new TT().new TimeRange(calRange.start, o.start));
                        calRange.start = o.end;
                        break;
                    }

                    if(o.start > calRange.start && o.end < calRange.end){
                        missedTimeRanges.add(new TT().new TimeRange(calRange.start, o.start));
                        calRange.start = o.end;
                        continue;
                    }
                }
            }

            if(!finded){
                missedTimeRanges.add(b);
            }else if (calRange.end - calRange.start > 0){
                missedTimeRanges.add(calRange);
            }
        }
        System.out.println(missedTimeRanges);
    }

    class TimeRange implements Comparable<TimeRange>{
        @Override
        public int compareTo(TimeRange o) {
            return this.start - o.start;
        }

        int start;
        int end;
        TimeRange(int start,int end){
            this.start = start;
            this.end = end;
        }

        @Override
        public String toString() {
            return "["+start+","+end+"]";
        }
    }

    public static void cru(int[][] o, int[][] b, int n) {

        while (o.length > 0) {
            if (n > b.length-1) {
                System.out.println(Arrays.toString(o[0]));
                LinkedList<int[]> newO = new LinkedList<>(Arrays.asList(o));
                newO.removeFirst();
                o = newO.toArray(new int[newO.size()][2]);
            } else {
                int[] oc = o[0];
                int[] bc = b[n];
                if (oc[1] <= bc[1]) {
                    if (oc[1] <= bc[0]) {
                        System.out.println(Arrays.toString(oc));
                    } else if (oc[0] < bc[0]) {
                        int[] temp = new int[]{oc[0], bc[0]};
                        System.out.println(Arrays.toString(temp));
                    }
                    LinkedList<int[]> newO = new LinkedList<>(Arrays.asList(o));
                    newO.removeFirst();
                    o = newO.toArray(new int[newO.size()][2]);
                } else {
                    n++;
                    LinkedList<int[]> newO = new LinkedList<>(Arrays.asList(o));

                    if (oc[0] < bc[1]) {
                        if (oc[0] < bc[0]) {
                            int[] temp = new int[]{oc[0], bc[0]};
                            System.out.println(Arrays.toString(temp));
                        }
                        newO.removeFirst();
                        newO.addFirst(new int[]{bc[1], oc[1]});
                    }
                    o = newO.toArray(new int[newO.size()][2]);
                    cru(o, b, n);
                }
            }
        }
    }
}
